package com.asset.controller;

import com.asset.entity.Purchase;
import com.asset.entity.approval.Phase;
import com.asset.entity.approval.Process;
import com.asset.service.ApprovalService;
import com.asset.vo.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 审批控制层
 * 这里还是以购置表为例子
 * 购置表的单价超过一定限度就要层层审批
 */
@RestController
@RequestMapping("/approval")
public class ApprovalController {
    @Autowired
    private ApprovalService approvalService;

    /**
     * 根据部门和金额以及申请审批的类型判断审批的流程
     *
     */
    @RequestMapping("getProcessPurchase")
    public AjaxJson getProcessPurchase(Purchase purchase) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            Process process = approvalService.getProcessPurchase(purchase);
            ajaxJson.putData("process", process);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxJson;
    }

    /**
     * 往审批阶段表里添加阶段
     */
    @RequestMapping("addPhasePurchase")
    public AjaxJson addPhasePurchase(Purchase purchase,Process process) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            approvalService.addPhasePurchase(purchase,process);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxJson;
    }

    /**
     *根据流程现在所处阶段产生申请记录表
     * 根据购置编号找到流程阶段
     */
    @RequestMapping("addRecordPurchase")
    public AjaxJson addRecordPurchase(Purchase purchase, Phase phase) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            //添加记录
            approvalService.addRecordPurchase(purchase,phase);
        }catch (Exception e) {
            e.printStackTrace();

        }
        return ajaxJson;
    }
}
